export function guid() {
  return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(
      /[xy]/g,
      function (c) {
          var r = (Math.random() * 16) | 0,
              v = c == "x" ? r : (r & 0x3) | 0x8;
          return v.toString(16);
      }
  );
}

export function bigNumberFormat(num: number) {
  if (num > Number.MAX_SAFE_INTEGER || num < Number.MIN_SAFE_INTEGER) {
      return String(num).replace(/(\.[0-9]{4})[0-9]*e/, "$1e");
  } else if (num > 1e12) {
      return parseInt(String(num / 1e10)) / 100 + "T";
  } else if (num > 1e9) {
      return parseInt(String(num / 1e7)) / 100 + "G";
  } else if (num > 1e6) {
      return parseInt(String(num / 1e4)) / 100 + "M";
  } else if (num > 1e3) {
      return parseInt(String(num / 1e1)) / 100 + "K";
  } else {
      return num.toFixed(2);
  }
}

export function formatByte(num) {
  if (num >= 1048576) {
      return parseInt(String(num / 1048576 * 100)) / 100 + "M";
  } else if (num >= 1024) {
      return parseInt(String(num / 1024 * 100)) / 100 + "K";
  } else {
      return String(num) + "B";
  }
}

/**
* number format：123,456,789
* @param num
* fractionDigits fraction length
* unitNumber 10000 unit format / 1000
* unit
*/
export function thousandSplitFormat(
  num: number = 0,
  fractionDigits = 0,
  unitNumber = 1,
  unit = ""
): number | string {
  if (num > unitNumber) {
      const cNum = (Number(num) / unitNumber).toFixed(fractionDigits);
      return `${cNum
          .toString()
          .replace(/(\d{1,3})(?=(\d{3})+(?:$|\.))/g, "$1,")}${unit}`;
  }

  return Number(num).toFixed(fractionDigits);
}

/**
 * 将数值四舍五入(保留2位小数)后格式化成金额形式
 * https://www.haorooms.com/post/js_qian_huobi_gs
 * @param num 数值(Number或者String)
 * @return 金额格式的字符串,如'1,234,567.45'
 * @type String
 */
 export function formatCurrency(num) {
  if (!num) {
    num = 0
  }
  num = num.toString().replace(/\$|\,/g, "")
  if (isNaN(num)) num = "0"
  let sign = num == (num = Math.abs(num))
  num = Math.floor(num * 100 + 0.50000000001)
  let cents = num % 100
  let centsAsStr = "" + cents
  num = Math.floor(num / 100).toString()
  if (cents < 10) {
    centsAsStr = "0" + cents
  }
  for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++) {
    num =
      num.substring(0, num.length - (4 * i + 3)) +
      "," +
      num.substring(num.length - (4 * i + 3))
  }
  return (sign ? "" : "-") + num + "." + centsAsStr
}

/**
 * 数字格式化：千分位逗号
 * @param num
 */
 export function numFormat(num: number = 0): number | string {
  if (num > 10000) {
    const cNum = (Math.round(num) / 10000).toFixed(2)
    return `${cNum.toString().replace(/(\d{1,3})(?=(\d{3})+(?:$|\.))/g, "$1,")}万`
  }
  num = Math.round(num)
  return num
}

/**
 * 百分数格式化
 * @param num
 * @param numberOfDigits
 * @param withUnit
 */
export function percentageNumFormat(
  num: number = 0,
  numberOfDigits: number = 1,
  withUnit: boolean = true,
) {
  if (withUnit) {
    return `${(num * 100).toFixed(numberOfDigits)}%`
  }
  return `${(num * 100).toFixed(numberOfDigits)}`
}

/**
 *  文字超出指定长度...
 * @param long 长度
 */
 export const textExceedsEllipsis = (text: string, long: number) => {
  if (text.length > long) {
    return `${text.substring(0, long - 1)}...`
  }
  return text
}

// 解析区间 []、()、[)、(]
export function parseInterval(str) {
  const match = /([\[\(]) *([^ ]*) *, *([^ ]*) *([\]\)])/.exec(str)
  if (match) {
    return {
      includeMin: match[1] === "[" ? true : false,
      min: match[2],
      max: match[3],
      includeMax: match[4] === "]" ? true : false,
    }
  }
}